home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / Dialog popups / Source / DialogTest.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  5.4 KB  |  201 lines  |  [TEXT/CWIE]

  1. // uses universal headers
  2. //
  3. // snippet to demonstrate the use of the system 7 popup 
  4. // control cdef in a program using modal dialogs
  5. //
  6. // Nick Thompson, 4/26/94
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10. /* 
  11. Sponge ID:105648       New Question
  12.   
  13. The content of your link dated: 31 May 1994 follows:
  14. ******************************************************************************
  15. ------------------------------------------------------------------------------
  16.  
  17. I’m not sure who I should refer this to, but I believe I have found a bug in
  18. the new Dialog Manager routines SetDialogDefaultItem & GetStdFilterProc.
  19.  
  20. What I am trying to do is create a default button that is initially grayed out
  21. and later enabled when the dialog box is filled in.  What is happening is that
  22. my button is getting re-enabled (unless I step into the source debugger).  My
  23. other non default button is getting grayed out as expected.  I tried
  24. rearranging the order that things were done to get it to stay gray, but I
  25. couldn’t.  I finally had to resort to using a filter proc and a global variable
  26. to tell me if I should gray the button after the initial call to
  27. GetStdFilterProc.
  28.   
  29. ******************************************************************************
  30.  
  31. */
  32.  
  33. //updates 8/96: Prefix.h added as prefix file for 68K and PPC MC projects;
  34. //old routine names changed
  35.  
  36. #include <Menus.h>
  37. #include <Processes.h>
  38. #include <Dialogs.h>
  39. #include <Fonts.h>
  40.  
  41. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit) ;
  42.  
  43. const char kEnter    = 0x03 ;
  44. const char kReturn    = 0x0D ;
  45. const char kEscape    = 0x1B ;
  46. const char kPeriod    = '.' ;
  47.  
  48. const char kThePopupMenu = 4 ;        // popup is the fourth item in the dialog
  49. const char kUserTextArea = 5 ;        // this is a rect for us to write a string
  50.                                     // with the item chosen in it
  51.  
  52.  
  53. // Structure for the private data for a popup control.
  54. // This structure is documented on page 5-77 
  55. // Inside Macintosh: Macintosh Toolbox Essentials
  56.  
  57. typedef struct popupPrivateData {
  58.     MenuHandle     mHandle;     // the popup menu handle 
  59.     short         mID;        // the popup menu ID 
  60.     // after these two public fields is the mPrivate private data, 
  61.     // which may be any old size and should not be messed with 
  62. }    popupPrivateData;
  63.  
  64.  
  65.  
  66.  
  67. void main(void) {
  68.  
  69.     DialogPtr             thePopupDialog ;
  70.     short                itemHit ;
  71.     popupPrivateData    **myPopupPrivateDataPtr ;
  72.     
  73.     short                iKind;
  74.     Handle                iHandle;
  75.     Rect                iRect;
  76.     
  77.     MenuHandle            thePopupMenuHdl ;
  78.     Str255                theItem ;
  79.     
  80.     OSErr                 theErr ;
  81.     
  82.     // initialize the toolbox
  83.     InitGraf(&qd.thePort); InitFonts(); InitWindows(); InitMenus();
  84.     TEInit(); InitDialogs((long)nil); InitCursor(); FlushEvents(everyEvent,0);
  85.     
  86.     thePopupDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  87.  
  88.     SetPort( (GrafPtr)thePopupDialog ) ;
  89.     theErr =  SetDialogDefaultItem(thePopupDialog, ok) ;
  90.  
  91.     if( theErr != noErr )
  92.         ExitToShell() ;
  93.  
  94.     
  95.     do {
  96.     
  97.         ModalDialog ( NewModalFilterProc(OurFilter), &itemHit );
  98.         
  99.         if( itemHit == kThePopupMenu ) {
  100.         
  101.             // the user choose the popup.  The item number selected will be the control value
  102.             // we need to get the menuhandle associated with the control, it is in the private
  103.             // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  104.             
  105.             // get the control handle for the popup            
  106.             GetDialogItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  107.             
  108.             // extract from the control the menuhandle
  109.             myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  110.             thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  111.     
  112.             // get the string associated with the users selection
  113.             GetMenuItemText ( thePopupMenuHdl, GetControlValue((ControlHandle)iHandle), theItem );
  114.             
  115.             // get the rect we are drawing in
  116.             GetDialogItem ( thePopupDialog, kUserTextArea, &iKind, &iHandle, &iRect) ;
  117.             SetDialogItemText ( iHandle, theItem );
  118.             
  119.             // this ensures that the update handler in the filter proc is called
  120.             // as that is where we enable or disable the OK button
  121.             InvalRect( &iRect ) ;
  122.             
  123.             // reset itemHit to something else or we'll continually redraw
  124.             itemHit = 0 ;
  125.         
  126.         }
  127.         
  128.     } while( itemHit != ok ) ;
  129.     
  130.     DisposeDialog ( thePopupDialog );
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit)
  139. {
  140.  
  141.     ModalFilterUPP         theProc ;
  142.     Boolean                retVal ;
  143.     
  144.     static    Boolean        isDisabled = false ;
  145.     OSErr                theErr = noErr ;
  146.     
  147.     // stuff for getditems etc
  148.     Str255                theItem ;
  149.     short                iKind;
  150.     Handle                iHandle;
  151.     Rect                iRect;
  152.     
  153.     
  154.     // get the std filter proc
  155.     
  156.     theErr = GetStdFilterProc( &theProc ) ;
  157.     
  158.     if( theErr != noErr )
  159.         ExitToShell() ;
  160.         
  161.     // try to call the standard filter, if it handles the event, we don't
  162.     if( !(retVal = CallModalFilterProc(theProc, dlg, event, itemHit)) )
  163.     {
  164.         switch (event->what) {
  165.     
  166.             case nullEvent:
  167.                 break;
  168.     
  169.             case keyDown:
  170.             case autoKey:
  171.                 retVal = false;
  172.                 break ;
  173.     
  174.             case updateEvt:
  175.                 // get the text item we are drawing in
  176.                 GetDialogItem ( dlg, kUserTextArea, &iKind, &iHandle, &iRect) ;
  177.                 GetDialogItemText ( iHandle, theItem );
  178.                 isDisabled = theItem[0] == '\0' ;
  179.                 
  180.                 // enable or disable the OK button depending on whether 
  181.                 // we made a selection yet
  182.                 GetDialogItem(  dlg, ok, &iKind, &iHandle, &iRect) ;
  183.                 if( isDisabled )
  184.                     HiliteControl( (ControlHandle)iHandle, 255 ) ;
  185.                 else
  186.                     HiliteControl( (ControlHandle)iHandle, 0 ) ;
  187.                     
  188.                 retVal = false;
  189.                 break ;
  190.     
  191.             default:
  192.                 retVal = false;
  193.                 break ;
  194.         }
  195.     }
  196.     
  197.     return retVal ;
  198. }
  199.  
  200.  
  201.